package com.njwb.io;

import java.io.*;

/**
 * Description: 类的描述
 * Author: yuandeyin@itany.com
 * Date: 2025-08-06 09:07
 * Version: 1.0
 */
public class TestInputstreamReaderAndOutputStreamWriter {
    public static void main(String[] args) {
        InputStream in = null;
        OutputStream out = null;
        InputStreamReader inputStreamReader = null;
        OutputStreamWriter outputStreamWriter = null;
        try {
            //已知两个字节流
            //希望采用字符流处理
            //将字节流转换成字符流
            in = new FileInputStream("src/com/njwb/io/hello.txt");
            out = new FileOutputStream("src/com/njwb/io/hello.txt.bak");

            inputStreamReader = new InputStreamReader(in);
            outputStreamWriter = new OutputStreamWriter(out);

            //字节流的操作方式
//            byte[] buffer = new byte[3];
//            int length = in.read(buffer);
//            while (length != -1){
//                System.out.println(new String(buffer,0,length));
//                out.write(buffer,0,length);
//                out.flush();
//                length = in.read(buffer);
//            }

            //修改成字符流操作
            char[] buffer = new char[3];
            int length = inputStreamReader.read(buffer);
            while(length != -1){
                System.out.println(new String(buffer,0,length));
                outputStreamWriter.write(buffer,0,length);
                outputStreamWriter.flush();
                length = inputStreamReader.read(buffer);
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //.....
        }
    }

}
